//
//  Pet_RecordViewController.swift
//  PetApp
//
//  Created by mac on 2023/9/18.
//  Copyright © 2023 Pet_. All rights reserved.
//

import UIKit

class Pet_RecordViewController: BasicViewController,NaviBarHidden {

    let topI = UIImageView.initImgView(img: "titttle_my p")
    let bannerI = UIButton.initV(img: "icon_top12")
    private var ttleL = UILabel.initV(.setTypeFace(22.Fit,type: .bold), text: "Reminder", textColor:.black)
    let addB1 = UIButton.initV(img: "icon_post")
    let imgArr = ["icon_health","icon_wash","icon_food","icon_others"]
    var datasource:[InfoUtil.recordDta]? = []
    
    private lazy var itemsTableView: UITableView = {
        let view = UITableView(frame: .zero, style: .plain)
        view.separatorStyle = .none
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = .clear
        view.register(RecordCell.self, forCellReuseIdentifier: RecordCell.description())
        view.rowHeight = 100.Fit
        return view
    }()
    let backV = UIView.init(frame: .zero)

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.datasource = Pet_RecordManager.shared.RecordList()
        print("======datasource ")
        print(self.datasource)
        self.itemsTableView.reloadData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.subviews {
            topI
            bannerI
            backV.subviews {
                ttleL
                addB1
                itemsTableView
            }
        }
        
        topI.width(screenWidth).height(60.Fit).top(status_bar_height() + 15.Fit)
        topI.centerHorizontally()
        
        bannerI.isUserInteractionEnabled = true
        bannerI.width(345.Fit).height(175.Fit)
        bannerI.Top == topI.Bottom + 30.Fit
        bannerI.centerHorizontally()
        bannerI.addAct {
            let vc = Pet_StoryViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
        backV.backgroundColor = .white
        backV.left(0).right(0).bottom(0)
        backV.Top == bannerI.Bottom + 20.Fit
        backV.layer.maskedCorners = [.layerMaxXMinYCorner,.layerMinXMinYCorner]
        backV.layer.cornerRadius = 20.Fit
        
        ttleL.left(15.Fit).right(15.Fit).height(30.Fit).top(20.Fit)
        addB1.size(50.Fit).right(35.Fit).top(0)
        addB1.addAct {
            let vc = Pet_AddRecordViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
        itemsTableView.left(0.Fit).right(0.Fit).bottom(0.Fit)
        itemsTableView.Top == addB1.Bottom + 15.Fit
        itemsTableView.emptyGameView(.noList)
        
    }
    
}


extension Pet_RecordViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datasource?.count ?? 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: RecordCell.description(), for: indexPath) as! RecordCell
        cell.model = datasource?[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = Pet_RecordDetailViewController()
        vc.model = datasource?[indexPath.row]
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

class RecordCell: UITableViewCell {
    
    let backV = UIView.init(frame: .zero)
    let iconImg = UIImageView.initImgView(img: "")
    let moreB = UIButton.initV(img: "more111")
    private var nameL = UILabel.initV(.setTypeFace(16.Fit,type: .medium), text: "", textColor:titleColor)
    private var timeL = UILabel.initV(.setTypeFace(12,type: .medium), text: "", textColor:.color(hexString: "#666666"))
    let statusI = UIImageView.initImgView(img: "picture_progress")
    
    var model:InfoUtil.recordDta?{
        didSet{
            
            self.nameL.text = model?.desc
            self.timeL.text = "Deadline: " + (model?.time ?? "")
            let str = "icon_" + (model?.type ?? "")
            self.iconImg.image = .init(named:str)
            
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy/MM/dd"
            
            let now = Date()
            let dateString = dateFormatter.string(from: now) // 将 Date 对象转换为字符串
            let noewDate = dateFormatter.date(from:dateString)
            let timeInterval:TimeInterval = noewDate!.timeIntervalSince1970
            let timeStamp = Int(timeInterval)
                                
            let recordDate = dateFormatter.date(from: model?.time ?? "")
            let RecordtimeInterval:TimeInterval = recordDate?.timeIntervalSince1970 ?? 0
            let RecordtimeStamp = Int(RecordtimeInterval)
            if RecordtimeStamp >= timeStamp{
                statusI.image = .init(named: "picture_progress")
            }else{
                statusI.image = .init(named: "picture_dead")
            }
            if model?.done == "1"{
                statusI.image = .init(named: "picture_done")
            }
            
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
        contentView.subviews {
            backV.subviews {
                iconImg
                nameL
                timeL
                statusI
            }
        }
        
        backV.left(15.Fit).right(15.Fit).top(15.Fit).bottom(0)
        backV.backgroundColor = mainColor.withAlphaComponent(0.3)
        backV.setAllCorners(corner: 18.5.Fit)
        
        statusI.right(0).top(0).bottom(0).width(80.Fit)
        iconImg.size(40.Fit).left(15.Fit)
        iconImg.centerVertically()
        
        nameL.top(15.Fit).right(20.Fit).height(25.Fit)
        nameL.Leading == iconImg.Trailing + 10.Fit
        nameL.numberOfLines = 0
        
        timeL.right(20.Fit).height(25.Fit)
        timeL.Leading == iconImg.Trailing + 10.Fit
        timeL.Top == nameL.Bottom
        
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

